package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.HelpInfo;
import com.qst.service.HelpInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller @RequestMapping("/help") public class HelpInfoController {
	 @Autowired private HelpInfoService helpInfoService;

	 @RequestMapping("/form") public String helpform(Model model, @Param("id") Integer id) {
		  String url = "../helpinfo/addhelpinfo.jsp";
		  HelpInfo helpInfo = helpInfoService.findByid(id);
		  model.addAttribute("helpInfo", helpInfo);
		  model.addAttribute("mainPage", url);
		  return "common/main";
	 }

	 @RequestMapping("/view") public String viewform(Model model, @Param("id") Integer id) {
		  HelpInfo helpInfo = helpInfoService.findByid(id);
		  model.addAttribute("helpInfo", helpInfo);
		  model.addAttribute("mainPage", "../helpinfo/viewhelpinfo.jsp");
		  return "common/main";
	 }

	 @RequestMapping("/delete") public String deleteform(Model model, @Param("id") Integer id) {
		  helpInfoService.deleteInfo(id);
		  return "redirect:/help/infolist.action";
	 }

	 @RequestMapping("/addorupdate") public String addhouse(Model model, HelpInfo helpInfo) {
		  HelpInfo inf = helpInfoService.findByid(helpInfo.getId());
		  if (inf != null) {
				inf.setContent(helpInfo.getContent().replace("\r\n", "<br/>"));
				inf.setTitle(helpInfo.getTitle());
				inf.setInfotype(helpInfo.getInfotype());
				helpInfoService.updateInfo(inf);
		  } else {
				helpInfo.setCreatedate(new Date());
				helpInfo.setContent(helpInfo.getContent().replace("\r\n", "<br/>"));
				helpInfoService.insertInfo(helpInfo);
		  }

		  model.addAttribute("mainPage", "../helpinfo/infolist.jsp");
		  return "redirect:/help/infolist.action";
	 }

	 @RequestMapping("/infolist") public String infoList(Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		  PageHelper.startPage(page, pageSize);
		  List<HelpInfo> infolist = helpInfoService.findAllInfo();
		  PageInfo<HelpInfo> p = new PageInfo<HelpInfo>(infolist);
		  model.addAttribute("infolist", infolist);
		  model.addAttribute("p", p);
		  model.addAttribute("mainPage", "../helpinfo/infolist.jsp");
		  return "common/main";
	 }
}
